<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人博客列表页</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_list.css">
</head>

<body>
    <!-- 导航栏、左侧用户信息区域、右侧博客列表区域 -->
    <!-- 导航栏 -->
    <div class="nav">
        <!-- logo -->
        <img src="./image/logo.png" alt="logo" id="myphoto">
        <!-- 标题 -->
        <span class="head">我的博客系统</span>
        <!-- 空白占位符 -->
        <span class="spacer">欢迎来到知识海洋</span>
        <!-- 右侧的几个连接 -->
        <a href="blog_list.html">首页</a>
        <a href="#" id="logout" class="logout">退出</a>
        <a href="blog_register.html">注册</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="left">
            <!-- 个人信息卡片 -->
            <div class="card">
                <img src="" alt="我的头像" id="myphotos">
                <h3></h3>
                <h6></h6>
                <div class="counter">
                    <a target="_blank" id="gits">我的Gitee</a>
                </div>
                <div class="counter">
                    <a href="user_update.html">修改个人资料</a>
                </div>
                <h5 id="reader"></h5>
                <h5 id="gooder"></h5>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="right">
        </div>
    </div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        //处理退出登录请求
        let logoutD = document.querySelector(".logout");
        logoutD.onclick = function () {
            jQuery.getJSON("login/out", {},
                function (data) {
                    if (data.status == 1) {
                        alert(data.message);
                        window.location.href = 'blog_login.html'
                    }
                }
            )
        }
        //使用ajax请求，向服务器获取当前用户的登录状态
        jQuery.getJSON("home/myMse", {},
            function (obj) {
                if (obj.status == -1) {
                    alert(obj.message);
                    window.location.href = 'blog_update.html';
                } else {
                    buildUser(obj.data);
                }
            }
        )

        let checkButton = document.createElement("input");
        checkButton.type = "button";
        checkButton.value = "一键审核所有带有敏感词的文章";
        checkButton.id = "check";
        checkButton.className = "good";
        checkButton.onclick = check;


        function buildUser(user) {
            //根据服务器返回的用户信息，来修改页面的显示内容
            let h3 = document.querySelector('.card>h3');
            h3.innerHTML = user.username;
            let h6 = document.querySelector('.card>h6');
            h6.innerHTML = user.signature;
            let gitee = document.querySelector('#gits');
            gitee.href = user.gitee;
            let photo = document.querySelector('#myphotos');
            photo.src = user.img;
            if (user.identity == 0) {
                let addArticle = document.createElement("a");
                addArticle.type = "a";
                addArticle.id = "addArticle";
                addArticle.innerHTML = "发布文章";
                addArticle.className = "counter";
                addArticle.href = "blog_edit.html";
                let leftDiv = document.querySelector('.container .left .card');
                leftDiv.appendChild(addArticle);
                let read = document.querySelector('#reader');
                read.innerHTML = "访问量: " + user.reader;
                let good = document.querySelector('#gooder');
                good.innerHTML = "获赞量: " + user.gooder;
            } else {
                let leftDiv = document.querySelector('.container .left .card');
                leftDiv.appendChild(checkButton);
            }
        }
        //使用ajax请求，向服务器获取博客列表，按照时间降序排列
        jQuery.getJSON("home/myblogList" + location.search, {},
            function (ret) {
                if (ret.status == -1) {
                    alert(ret.message);
                } else {
                    buildMyBlogs(ret.data);
                }
            }
        )

        function check() {
            $.ajax({
                type: 'post',
                url: 'home/checkByIk',
                success: function (respData) {
                    if (respData.status == 1) {
                        alert(respData.message);
                    } else {
                        alert(respData.message);
                    }
                },
                error: function () {
                    $.toast({
                        heading: '错误',
                        text: '访问出现问题，请与管理员联系.',
                        icon: 'error'
                    });
                }
            })
        }

        //根据响应中的body数据构造HTML内容
        function buildMyBlogs(blogs) {
            let rightDiv = document.querySelector('.container .right');
            //遍历blogs，根据每个遍历的结果，构造出一个html片段，并且添加到页面中
            for (let blog of blogs) {
                //blog就是一个json对象，里面的内容就和服务器响应的json是匹配的
                //创建出div.blog
                let blogDiv = document.createElement("div");
                blogDiv.className = 'blog';
                //创建出div.title
                let titleDiv = document.createElement("div");
                titleDiv.className = 'title';
                titleDiv.innerHTML = blog.title;
                blogDiv.appendChild(titleDiv);
                //创建出div.date
                let dateDiv = document.createElement("div");
                dateDiv.className = 'date';
                dateDiv.innerHTML = blog.postTime;
                blogDiv.appendChild(dateDiv);
                //创建出div.desc（正文）
                let descDiv = document.createElement("div");
                descDiv.className = 'desc';
                descDiv.innerHTML = blog.content;
                blogDiv.appendChild(descDiv);
                //创建出div.mess
                let messDiv = document.createElement("div");
                messDiv.className = 'mess';
                //创建出阅读量span
                let readDiv = document.createElement('Div');
                readDiv.className = 'read';
                readDiv.innerHTML = '阅读量:' + blog.consult;
                messDiv.appendChild(readDiv);
                //创建出获赞量span
                let praiseDiv = document.createElement("Div");
                praiseDiv.className = 'good';
                praiseDiv.innerHTML = '获赞量:' + blog.praise;
                messDiv.appendChild(praiseDiv);
                blogDiv.appendChild(messDiv);
                //创建出a.detail（博客详情链接)
                let detailA = document.createElement("a");
                detailA.className = 'detail';
                detailA.innerHTML = '查看全文 &gt; &gt;';
                detailA.href = 'blog_detail.html?blogId=' + blog.id;
                blogDiv.appendChild(detailA);
                rightDiv.appendChild(blogDiv);
            }
        }
    </script>
</body>

</html>